package com.tanhua.server.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.server.pojo.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
@Service
public class UserService {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${tanhua.sso.url}")
    private String url;

    private static final ObjectMapper MAPPER = new ObjectMapper();

    //根据用户token查询用户信息
    public User queryUserByToken(String token) {
        //调用sso层接口获取用户信息
        String json = restTemplate.getForObject(url + "/user/{token}", String.class, token);

        //非空判断
        if (StringUtils.isNotEmpty(json)) {
            try {
                //将json转换成user对象返回
                User user = MAPPER.readValue(json, User.class);
                return user;
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return null;
    }
}
